/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.ChiTietPhieuNhapDAO;
import dao.QuiDinhDAO;
import dao.SanPhamDAO;
import dao.BaoCaoTonDAO;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.*;

/**
 *
 * @author LaiHuynh
 */
public class ChiTietPhieuNhap extends javax.swing.JInternalFrame {

    private int maCuaHang;
    private Date d;

    public Date getD() {
        return d;
    }

    public void setD(Date d) {
        this.d = d;
    }
    

    public int getMaCuaHang() {
        return maCuaHang;
    }

    public void setMaCuaHang(int maCuaHang) {
        this.maCuaHang = maCuaHang;
    }
    
    private DefaultTableModel dtm;
    private int maPhieuNhap;

    public int getMaPhieuNhap() {
        return maPhieuNhap;
    }

    public void setMaPhieuNhap(int maPhieuNhap) {
        this.maPhieuNhap = maPhieuNhap;
    }
    
    /**
     * Creates new form ChiTietPhieuNhap
     */
    public ChiTietPhieuNhap() {
        initComponents();
    }
    
    private void hienThiDanhSach() {
         //doc du lieu
        this.dtm = new DefaultTableModel();
        List<pojo.ChiTietPhieuNhap> ds = ChiTietPhieuNhapDAO.layDanhSachChiTietPhieuNhap();
        this.dtm.addColumn("Mã Phiếu Nhập");
        this.dtm.addColumn("Mã Sản Phẩm");
        this.dtm.addColumn("Số Lượng");
        for(int i=0; i<ds.size(); i++){ 
            pojo.ChiTietPhieuNhap ctpn=ds.get(i); 
            Vector vt = new Vector();
            PhieuNhap pn = ctpn.getPhieuNhap();
            vt.add(pn.getMaPhieuNhap());
            SanPham sp = ctpn.getSanPham();
            vt.add(sp.getMaSanPham());
            vt.add(ctpn.getSoLuongNhap());
            dtm.addRow(vt);
        }
        this.jtDanhSach.setModel(dtm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDanhSach = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfMaPhieuNhap = new javax.swing.JTextField();
        jtfMaSanPham = new javax.swing.JTextField();
        jtfSoLuong = new javax.swing.JTextField();
        jbtMaSanPham = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jbtBongSung = new javax.swing.JButton();
        jtfXoaTrang = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Chi tiết phiếu nhập");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Phiếu nhập chi tiết"));

        jtDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtDanhSach);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin bổ sung"));

        jLabel2.setText("Mã phiếu nhập:");

        jLabel3.setText("Mã sản phẩm:");

        jLabel4.setText("Số lượng:");

        jtfMaPhieuNhap.setEnabled(false);

        jtfMaSanPham.setEnabled(false);

        jbtMaSanPham.setText(">");
        jbtMaSanPham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtMaSanPhamActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfMaPhieuNhap)
                    .addComponent(jtfMaSanPham)
                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtMaSanPham))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfMaPhieuNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfMaSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtMaSanPham))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setText("BỔ SUNG CHI TIẾT");

        jButton4.setText("Đóng");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Các chức năng"));

        jbtBongSung.setText("Bổ xung");
        jbtBongSung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtBongSungActionPerformed(evt);
            }
        });

        jtfXoaTrang.setText("Xóa trắng");
        jtfXoaTrang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfXoaTrangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addComponent(jbtBongSung)
                .addGap(18, 18, 18)
                .addComponent(jtfXoaTrang)
                .addContainerGap(98, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtBongSung)
                    .addComponent(jtfXoaTrang))
                .addGap(0, 11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(121, 121, 121)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton4)
                .addGap(50, 50, 50))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton4)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        jtfMaPhieuNhap.setText(String.valueOf(this.getMaPhieuNhap()));
        this.hienThiDanhSach();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jbtMaSanPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtMaSanPhamActionPerformed
        // TODO add your handling code here:
        TimKiemSanPham_Dialog dialog = new TimKiemSanPham_Dialog(null, true);
        dialog.setVisible(true);
        int maSanPham = dialog.getMaSanPham();
        double donGia = dialog.getGiaBan();
        this.jtfMaSanPham.setText(String.valueOf(maSanPham));
    }//GEN-LAST:event_jbtMaSanPhamActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jbtBongSungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtBongSungActionPerformed
        // TODO add your handling code here:
        try {
            //them
            QuyDinh qd = QuiDinhDAO.layAllQuyDinh(maCuaHang);
            int nhapMin = qd.getSoLuongNhapItNhat();
            int luongTonMin = qd.getSoLuongTonItNhat();
            //end them
            int maPhieuNhap = Integer.parseInt(jtfMaPhieuNhap.getText());
            int maSanPham = Integer.parseInt(jtfMaSanPham.getText());
            int soLuong = Integer.parseInt(jtfSoLuong.getText());
            if(soLuong < nhapMin) {
                JOptionPane.showMessageDialog(this, "Số lượng phải lớn hơn số lượng quy định");
                return;
            }
            SanPham sp = SanPhamDAO.layThongTinSanPham(maSanPham);
            int luongTon = sp.getSoLuongTon();
            int luonngTonNew = luongTon + soLuong;
            if(luongTon >= luongTonMin) {
                JOptionPane.showMessageDialog(this, "Chỉ nhập các sản phẩm có lượng tồn ít hơn theo quy định");
                return;
            }
            sp.setSoLuongTon(luonngTonNew);
            SanPhamDAO.capNhatThongTinSanPham(sp);
            BaoCaoTon bct = new BaoCaoTon();
            bct.setSanPham(sp);
            bct.setNgayPhatSinh(d);
            bct.setTonDau(luongTon);
            bct.setPhatSinh(luonngTonNew - luongTon);
            bct.setTonCuoi(luonngTonNew);
            BaoCaoTonDAO.themThongBaoCaoTon(bct);
            pojo.ChiTietPhieuNhap ctpn = new pojo.ChiTietPhieuNhap();
            ChiTietPhieuNhapId id = new ChiTietPhieuNhapId();
            id.setMaPhieuNhap(maPhieuNhap);
            id.setMaSanPham(maSanPham);
            ctpn.setId(id);
            ctpn.setSoLuongNhap(soLuong);
            ChiTietPhieuNhapDAO.themChiTietPhieuNhap(ctpn);
            JOptionPane.showMessageDialog(this, "Thêm Thành Công");
            this.hienThiDanhSach();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, "Sản phẩm đã có trong phiếu nhập");
        }
    }//GEN-LAST:event_jbtBongSungActionPerformed

    private void jtfXoaTrangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfXoaTrangActionPerformed
        // TODO add your handling code here:
        jtfMaSanPham.setText("");
        jtfSoLuong.setText("");
    }//GEN-LAST:event_jtfXoaTrangActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtBongSung;
    private javax.swing.JButton jbtMaSanPham;
    private javax.swing.JTable jtDanhSach;
    private javax.swing.JTextField jtfMaPhieuNhap;
    private javax.swing.JTextField jtfMaSanPham;
    private javax.swing.JTextField jtfSoLuong;
    private javax.swing.JButton jtfXoaTrang;
    // End of variables declaration//GEN-END:variables
}
